/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.shsc.controller;

import com.bionic.shsc.common.Utils;
import com.bionic.shsc.facade.UserFacade;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {

    private UserFacade userFacade;

    private UserFacade getUserFacade() {
        if (userFacade == null) {
            userFacade = new UserFacade();
        };
        return userFacade;
    }

    //@RequestMapping(method = RequestMethod.POST)
    @RequestMapping(value = "/cabinet", method = RequestMethod.POST)
    ModelAndView login(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String email = request.getParameter("email");
        String pass = request.getParameter("password");
        int id = getUserFacade().loginExecute(email, pass);
        if (id >= 0) {
            request.getSession().setAttribute("userid", id);
            return new ModelAndView("cabinet", "userFacade", userFacade);
        } else // return error
        { 
           return new ModelAndView("redirect:/user/register.htm","message", "User not found - register or restore your password");
        }
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    ModelAndView register(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String message = (String) request.getAttribute("message");
        if (message != null) {
            return new ModelAndView("register","messsage",message);
        } else {
            return new ModelAndView("register");
        }
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    ModelAndView registerExec(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String name = request.getParameter("fullname");
        String email = request.getParameter("email");
        String pass = request.getParameter("password");
        String passHash = Utils.getHash(pass);
        String street = request.getParameter("street");
        String num = request.getParameter("num");
        String zip = request.getParameter("zip");

        if (getUserFacade().registerUser(name, email, passHash, street, num, zip)) {
            request.setAttribute("email", email);
            request.setAttribute("pass", pass);
            return new ModelAndView("forward:/user/cabinet.htm");
        } else {
            // хз  пока что тут
            return new ModelAndView("errorpage", "message", "Registration error");
        }
    }
}
